package com.green.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.green.system.common.AppUtils;
import com.green.system.common.Constant;
import com.green.system.common.DataGridView;
import com.green.system.domain.User;
import com.green.system.mapper.RoleMapper;
import com.green.system.mapper.UserMapper;
import com.green.system.service.UserService;
import com.green.system.vo.UserVo;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;


/**
 * Created with IntelliJ IDEA.
 * @Author: Green
 * @Date: 2021/10/18/8:32
 * @Description: 
 */


@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService{

    private Log log = LogFactory.getLog(UserServiceImpl.class);

    @Autowired
    private UserMapper userMapper ;

    @Autowired
    private RoleMapper roleMapper;

/*    @Autowired
    private DeptService deptService;

    为什么不这么写，因为做redis缓存时，这么写拿不到后续proxy代理类，只能拿到最开始设定的bean，无法使用缓存
    所以写了AppUtils这个类拿到IOC的容器，从里面拿需要的proxy代理类
    */

    @Override
    public User queryUserByLoginName(String loginName) {
        QueryWrapper<User> qw = new QueryWrapper<>();
        if (StringUtils.isBlank(loginName)){
            log.error("登录名不能为空");
            return null ;
        }

        //如果不做以上判断则会仅在数据库执行select * from sys_user;
        qw.eq("loginname",loginName);
        User user = userMapper.selectOne(qw);
        return user;

    }

    @Override
    public DataGridView queryAllUser(UserVo userVo) {
        IPage<User> page = new Page<>(userVo.getPage(),userVo.getLimit());
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type", Constant.USER_TYPE_NORMAL);
        queryWrapper.eq(null != userVo.getDeptid(),"deptid",userVo.getDeptid());
        queryWrapper.like(StringUtils.isNotBlank(userVo.getName()),"name",userVo.getName());
        queryWrapper.like(StringUtils.isNotBlank(userVo.getAddress()),"address",userVo.getAddress());
        queryWrapper.like(StringUtils.isNotBlank(userVo.getRemark()),"remark",userVo.getRemark());

        //先查后改！！！
        this.userMapper.selectPage(page,queryWrapper);
        //查询部门名称替换deptId
        List<User> records = page.getRecords();
//        DeptService deptService = AppUtils.getContext().getBean(DeptService.class);
//        for (User record : records) {
//            if (null != record.getDeptid()){
//                Dept dept = deptService.getById(record.getDeptid());
//                record.setDeptname(dept.getTitle());
//            }
//        }
        return new DataGridView(page.getTotal(),records);
    }

    @Override
    public User saveUser(User user) {
        this.userMapper.insert(user);
        return user;
    }

    @Override
    public User updateUser(User user) {
        this.userMapper.updateById(user);
        return user;
    }

    @Override
    public Integer queryUserMaxOrderNum() {
        return this.userMapper.queryUserMaxOrderNum();
    }

    @Override
    public void saveUserRole(Integer uid, Integer[] rids) {
        //根据用户ID删除角色和用户中间表的数据
        roleMapper.deleteRoleUserByUid(uid);
        if (null!=rids&&rids.length>0){
            for (Integer rid : rids) {
                this.userMapper.saveUserRole(uid,rid);
            }
        }
    }

    @Override
    public Integer queryUserMaxId() {
        return this.userMapper.queryUserMaxId();
    }

    @Override
    public boolean removeById(Serializable id) {
        roleMapper.deleteRoleUserByUid(id);
        return super.removeById(id);
    }
}
